package com.mall.shopping.services;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.mall.shopping.TbItemGoodService;
import com.mall.shopping.constants.ShoppingRetCode;
import com.mall.shopping.converter.ItemCartConverter;
import com.mall.shopping.converter.ProductConverter;
import com.mall.shopping.converter.TbItemConverter;
import com.mall.shopping.dal.entitys.Item;
import com.mall.shopping.dal.entitys.ItemCat;
import com.mall.shopping.dal.entitys.ItemDesc;
import com.mall.shopping.dal.persistence.ItemCatMapper;
import com.mall.shopping.dal.persistence.ItemDescMapper;
import com.mall.shopping.dal.persistence.ItemMapper;
import com.mall.shopping.dto.*;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import tk.mybatis.mapper.entity.Example;

import java.util.HashMap;
import java.util.List;

/**
 * @Author: lin
 * @Date: 2020/7/10 14:32
 */

@Service
public class TbItemGoodServiceImpl implements TbItemGoodService {

    @Autowired
    private ItemMapper itemMapper;

    @Autowired
    private TbItemConverter itemConverter;

    @Autowired
    private ItemCatMapper itemCatMapper;

    @Autowired
    private ItemCartConverter itemCartConverter;

    @Autowired
    private ProductConverter productConverter;

    @Autowired
    private ItemDescMapper itemDescMapper;

    /**
     * 获取商品列表
     * @param request
     * @return
     */
    @Override
    public QueryGoodListResponse getGoods(QueryGoodListRequest request) {
        QueryGoodListResponse response = new QueryGoodListResponse();
        HashMap<Integer, String> sortWay = new HashMap<>();
        sortWay.put(1, "asc");
        sortWay.put(-1, "desc");

        //检查参数
        try {
            request.requestCheck();
        } catch (Exception e) {
            response.setCode(ShoppingRetCode.REQUISITE_PARAMETER_NOT_EXIST.getCode());
            response.setMsg(ShoppingRetCode.REQUISITE_PARAMETER_NOT_EXIST.getMessage());
            return response;
        }
        Example example = new Example(Item.class);
        Example.Criteria criteria = example.createCriteria();
        //判断是否为空并设置参数
        if (request.getCid() != null) {
            criteria.andEqualTo("cid", request.getCid());
        }
        if (request.getPriceGt() != null) {
            criteria.andGreaterThanOrEqualTo("price", request.getPriceGt());
        }
        if (request.getPriceLte() != null) {
            criteria.andLessThanOrEqualTo("price", request.getPriceLte());
        }
        if (request.getSort() != null) {
            String sort = sortWay.get(request.getSort());
            example.setOrderByClause("price" + " " + sort);
        }
        //查询和分页
        PageHelper.startPage(request.getPage(), request.getSize());
        List<Item> items = itemMapper.selectByExample(example);
        //转换
        List<QueryGoodDto> data = itemConverter.QueryItems(items);
        PageInfo<QueryGoodDto> pageInfo = new PageInfo<>(data);
        response.setTotal(pageInfo.getTotal());
        response.setData(data);
        response.setCode(ShoppingRetCode.SUCCESS.getCode());
        response.setMsg(ShoppingRetCode.SUCCESS.getMessage());
        return response;
    }

    @Override
    public List<CategoryDto> getCategoryList(String sort) {
        HashMap<String, String> sortMap = new HashMap<>();
        sortMap.put("1", "asc");
        sortMap.put("-1", "desc");
        Example example = new Example(ItemCat.class);
        if (sort != null && !sort.equals("")){
            example.setOrderByClause("sort_order" + " " + sortMap.get(sort));
        }
        List<ItemCat> itemCats = itemCatMapper.selectByExample(example);
        return itemCartConverter.itemCatToList(itemCats);
    }

    @Override
    public GetProductByIdDto getProduct(Integer id) {
        Item item = itemMapper.selectByPrimaryKey(id);
        GetProductByIdDto response = productConverter.ItemToProduct(item);
        ItemDesc desc = itemDescMapper.selectByPrimaryKey(id);
        if (desc != null){
            response.setDetail(desc.getItemDesc());
        } else {
            response.setDetail("");
        }

        return response;
    }
}
